home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-04 | 4.8 KB | 165 lines | [TEXT/MPS ] |
- # **********************************************************************
- # File: Script 4.vu
- #
- # Purpose: to demonstrate task and library use
- # in structured scripting
- #
- # Prerequisites: This script assumes that DrawShapesVU is the
- # currently active application on the target computer.
- #
- # Note: Abbreviations are now used for all descriptor traits.
- # For example, instead of absolute: just a: is
- # used.
- #
- # Written by: David Gaxiola
- #
- # Copyright © 1992 by Apple Computer, Inc., all rights reserved.
- #
- # **********************************************************************
-
- # Use the StandardDialogs.vulib external library.
- Libraries 'StandardDialogs.vulib'; # libraries declaration
-
- # Begin task definitions. **********************************************
-
- # Tasks can have default values with their arguments.
- # Global variables must be marked as such, with the keyword global
- # or they will be treated as local variables.
-
- # **********************************************************************
- # Calculate the absolute difference between two numbers.
- task Diff( Num1, Num2 )
- begin
- theDifference := Num1 - Num2;
- if (theDifference < 0) do
- theDifference := -theDifference;
- return theDifference;
- end;
-
- # **********************************************************************
- # Select one of the available tools.
- task SelectTool( toolNum := 1, randomDraw := false )
- begin
- global gWindowDim;
- global gToolColumn;
- global gToolRow;
-
- if ( randomDraw ) do
- toolNum := Random( 2, 4 );
- move a:{ (gWindowDim[1] + gToolColumn),
- (gWindowDim[2] + gToolRow[toolNum]) };
- click;
- end;
-
- # **********************************************************************
- # Select one of the seven predefined colors available.
- task SelectColor(colorNum := 1, randomColor := false) begin
- global gColorAvail;
-
- if ( gColorAvail ) do
- begin
- if randomColor do
- colorNum := Random( 1, 7 );
- select [ menuItem o:colorNum m:[ menu t:'Colors' ]];
- end;
- end;
-
- # **********************************************************************
- # Draw a rectangle with the cursor, remembering that a minimum size is
- # needed.
- task DrawRandomShapeWithTool( )
- begin
- global gWindowDim;
- global gWindowMod;
-
- range1 := ( gWindowDim[1] + gWindowMod[1] );
- range2 := ( gWindowDim[2] + gWindowMod[2] );
- range3 := ( gWindowDim[3] + gWindowMod[3] );
- range4 := ( gWindowDim[4] + gWindowMod[4] );
- xStart := Random( range1, range3 );
- yStart := Random( range2, range4 );
- xStop := Random( range1, range3 );
- yStop := Random( range2, range4 );
-
- # DrawShapesVU needs a minimum size in order to draw a shape.
- # These lines compensate for this need by making the difference
- # between the starting and stopping positions a minimum of 40.
-
- while ( Diff( xStart, xStop) < 40 )
- begin
- xStart := Random( range1, range3 );
- xStop := Random( range1, range3 );
- end;
-
- while ( Diff( yStart, yStop ) < 40 )
- begin
- yStart := Random( range2, range4 );
- yStop := Random( range2, range4 );
- end;
-
- move a:{ xStart, yStart };
- pressMouse;
- Wait(1); # for slower targets
- move a:{ xStop, yStop };
- releaseMouse;
- end;
-
- # Begin main body. ***********************************************
- script Tutorial4Main(maxIteration := 4,
- saveAsFilename := 'Tutorial 4 Example.ds')
- begin
-
- # global variable definitions *************************************
- global gToolColumn := 18;
- global gToolRow := { 40, 80, 120, 160 };
- global gWindowMod := { 41, 21, -21, -21 };
- global gWindowDim := { };
- global gScreenDim := { };
- global gColorAvail := false;
-
- MouseSpeed( 18 );
- Patience( 4 );
- match [ screen r:?gScreenDim m:true ];
-
- # Check to see if the Color menu is available.
- # Here gColorAvail is first a string, but is transformed into a boolean.
- match[ menu t:?gColorAvail o:5 ];
- if ( gColorAvail = 'Colors' ) do
- begin
- gColorAvail := true;
- end;
- else
- begin
- gColorAvail := false;
- end;
-
- # Create a new document and set it into position.
- println "Setting up new DrawShapes document.";
- select [ menuItem t:'New' m:[ menu t:'File' ]];
- drag [ window t:/Untitled-≈/ o:1 ] a:{ 1, 21 };
- size [ window t:/Untitled-≈/ o:1 ] w:(gScreenDim[3] - 2) h:(gScreenDim[4] - 22);
- match [ window r:?gWindowDim o:1 ];
-
- # Perform a while...do loop instead of a for loop.
- # The result is the same. Note the cleanliness because
- # of task usage.
- currentIteration := 1;
- while ( currentIteration <= maxIterations ) do
- begin
- println "Drawing iteration {currentIteration}.";
- SelectTool( 1, true );
- DrawRandomShapeWithTool( );
- SelectColor( 1, true );
- currentIteration := currentIteration + 1;
- end;
- SelectTool( 3 );
- DrawRandomShapeWithTool( );
- SelectColor( 4 );
-
- # Use the task DoSaveAs from the library StandardDialogs.vulib
- println "Saving and closing window.";
- DoSaveAs( saveAsFilename );
- close [ window t:saveAsFilename o:1 ]!;
- end;
- # End Main execution. *************************************************
-